package de.herberlin.pss.action;

import org.apache.log4j.Logger;

import de.herberlin.pss.ui.Frame;

public abstract class AbstractProgressDialogAction extends Thread implements Action {

	protected Logger logger = Logger.getLogger(getClass());

	public final void perform(){
		boolean nested=false;
		if (Frame.isInProgress()) {
			nested=true;
		}
		if (!nested) {
			Frame.startProgress();
		}
		Thread.yield();
		logger.debug("Start perform");
		try {
			doPerform();
		} catch (Exception e) {
			logger.error(e,e);
			Frame.showMessage(e.getLocalizedMessage(), e);
			rollback();
		} finally {
			logger.debug("End perform");
			if (!nested) {
				Frame.stopProgress();
			}
			Thread.yield();
		}
		
	}
	public void run() {
		perform();
	}

	protected abstract void doPerform() throws Exception;
	protected abstract void doRollback() throws Exception;
	public final void rollback(){
		logger.debug("Start rollback");
		try {
			doRollback();
		} catch (Exception e) {
			logger.error(e,e);
			Frame.showMessage(e.getLocalizedMessage(), e);
		} finally {
			logger.debug("End perform");
		}
	}

}
